﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogic;
using BusinessObject;
using Utility;
namespace WebTeamplate
{
    public partial class Admin_SubCategoryDetail : BasePage
    {
        #region Fields
        public string urlList = "SubCategory.aspx";       
        public string Command
        {
            get
            {
                object o = ViewState["Command"];
                if (o != null) return (string)o;
                return string.Empty;
            }
            set { ViewState["Command"] = value; }
        }
        public int SubCategoryID
        {
            get
            {
                object o = ViewState["SubCategoryID"];
                if (o != null) return (int)o;
                return int.MinValue;
                //a default
            }
            set { ViewState["SubCategoryID"] = value; }

        }
        #endregion

        #region Init,Loads
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                InitParamater();
                CommonFunction.LoadCategory(drlCategory, string.Empty, true);
                LoadData();
            }
        }
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            InitControl();
        }
        #endregion

        #region Methods
        public void ReturnList()
        {
            Response.Redirect(urlList + "?" + this.GetPageParamater());
        }
        private void LoadData()
        {
            SubCategory objCate = SubCategoryUtil.Get(this.SubCategoryID, CommonFunction.GetValueFromQueryString(this.Page, "F_drlCategory"));
            if (objCate != null)
            {
                txtName.Text = objCate.SubCategoryName;
                drlCategory.SelectedValue = objCate.CategoryId.ToString();
                chkIsActive.Checked = objCate.IsPublished;
            }
        }
        private void InitParamater()
        {
            this.SubCategoryID = ConvertValueObject.ParseInt(CommonFunction.GetValueFromQueryString(this.Page, "subcategoryId"));
            this.Command = CommonFunction.GetValueFromQueryString(this.Page, "act");
        }
        private void InitControl()
        {
            string validateGroup = "cate";
            btnSave.Text = Resources.Resource.SaveButtonText;
            refvName.ErrorMessage = string.Format(Resources.Resource.MessageRequireInput, CommonFunction.GetTextLabel(lblName));
            refvName.ValidationGroup = validateGroup;
            refvCategory.ErrorMessage = string.Format(Resources.Resource.MessageRequireInput, CommonFunction.GetTextLabel(lblCategory));
            refvCategory.ValidationGroup = validateGroup;
            vsumCategory.ValidationGroup = validateGroup;
            btnSave.ValidationGroup = validateGroup;
        }
        private void Update(SubCategory objData)
        {
            objData.SubCategoryId = this.SubCategoryID;
            if (!SubCategoryUtil.Update(objData))
                lblErrorMessage.Text = Resources.Resource.MessageError;
            else
                ReturnList();
        }
        private void Insert(SubCategory objData)
        {
            if (!SubCategoryUtil.Insert(objData))
                lblErrorMessage.Text = Resources.Resource.MessageError;
            else
                ReturnList();
        }
        #endregion

        #region Events
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                //SystemEnum.Category g = (SystemEnum.Category)this.drlCategory.SelectedValue.ToString() as SystemEnum.Category;
                SubCategory objData = new SubCategory();
                objData.IsPublished = chkIsActive.Checked;
                objData.CategoryId = ConvertValueObject.ParseInt(drlCategory.SelectedValue);
                objData.SubCategoryName = txtName.Text;
                objData.CreatedBy = objData.LastUpdatedBy = this.userLoginName;
                objData.CreatedDate = objData.LastUpdatedDate = CommonFunction.Now;
                if (this.Command != CommonFunction.EdityCommand)
                    Insert(objData);
                else
                    Update(objData);
            }
            else
            {
                lblErrorMessage.Text = Resources.Resource.MessageError;
            }
        }
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            ReturnList();
        }       
        protected void btnReset_Click(object sender, EventArgs e)
        {
            Response.Redirect("SubCategoryDetail.aspx" + "?" + this.GetPageParamater());
        }
        #endregion
    }
}
